﻿#include "GLLInc.h"
#include "GLLVirtualWorld.h"
#include "GLLLevelManager.h"
#include "GLLOSApp.h"
#include "GLLConfig.h"

namespace GLL
{

	void VirtualWorld::Create()
	{
		_lvlMgr = GLL_NEW LevelManager();
		if(!_lvlMgr) GLL_Throw("Create Level Manager failed in Virtual World Creation");

		DoCreate_spec();
		AutoSetQuality();
	}

	void VirtualWorld::Destory()
	{
		DoDestroy_spec();
		if(_lvlMgr){
			_lvlMgr->DestroyAll();
			GLL_DEL(_lvlMgr);
		}
	}

	void VirtualWorld::AutoSetQuality()
	{
		for( _quality = WQ_QUALITY; _quality >= WQ_FASTEST;)
		{
			bool suc = SetQuality_spec(_quality);
			if(suc) return;
			_quality = Quality(int(_quality) - 1);
		}
		_quality = WQ_DISABLED;
	}

	void VirtualWorld::Update(TimeMS ms)
	{
		RenderOneFrame_spec();
		_lvlMgr->Update(ms);
		//all cameras:
		Update_AllObjects(ms);
	}

	CamSrdPtr VirtualWorld::CreateCamera()
	{
		int index= _objects.size() + 1;
		ObjSrdPtr ptr = _CreateObject(Object::ID(Object::OT_Camera, index));
		CamSrdPtr pCam = boost::dynamic_pointer_cast<ObjectCamera>(ptr);
		pCam->_index = index;
		return pCam;		
	}

	CamSrdPtr VirtualWorld::GetCamera( Index i)
	{
		Object::ID oid(Object::OT_Camera, i);
		ObjSrdPtr ptr = _FindObject(oid);
		CamSrdPtr pCam = boost::dynamic_pointer_cast<ObjectCamera>(ptr);
		return pCam;				
	}

	void VirtualWorld::DestroyCamera( Index i)
	{
		Object::ID oid(Object::OT_Camera, i);
		_DestroyObject(oid);		
	}
} //GLL